package com.league.zjxn.main

import com.blankj.utilcode.util.BarUtils
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.MessageViewModel
import com.league.zjxn.main.adapter.MessageAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerAbstractFragment
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_main_message.*

class MainMessageFragment : OwnerViewModeFragment<MessageViewModel>() {

    companion object {
        fun newInstance(): MainMessageFragment {
            return MainMessageFragment()
        }
    }

    private val mMessageAdapter = MessageAdapter()

    override fun viewModel(): MessageViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_message

    override fun initView() {
        BarUtils.addMarginTopEqualStatusBarHeight(flMainMessageBar)

        rvMainMessageBody.adapter = mMessageAdapter

    }

    override fun initData() {
        viewModel.messageList()
    }

    override fun initViewMode() {
        viewModel.messageListLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data?.data
                ?: return@observe
            mMessageAdapter.setNewInstance(data)
        })
        viewModel.messageReadLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.msg)
        })
    }
}